/**
 * Created by ChangJiabo
 * on 19-5-28 下午2:17.
 */
public class LongestPalindrome {

    public String longestPalindrome(String s) {
        int maxLength = s.length();
        for (int i = maxLength; i > 0; i--) {
            for (int j = 0; j < maxLength - i + 1; j++) {
                if (checkPalindrome(s.substring(j, j + i))) {
                    return s.substring(j, j + i);
                }
            }
        }
        return "";
    }

    private boolean checkPalindrome(String s) {
        for (int i = 0; i < s.length() / 2; i++) {
            if (s.charAt(i) != s.charAt(s.length() - i - 1)) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        String s = "babadac";
       System.out.println(new LongestPalindrome().longestPalindrome(s));
    }
}
